<?php
class GroupManageController extends CAdmin {
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 组员管理
	 * Enter description here ...
	 */
	public function grounp_manage(){
		$parentid = $_SESSION['userid'];
		$where = "userid in (select userid from r_user_group where parentid = ".$parentid.")";
		$admindb = new Admin_Model();
		$info = $admindb->select($where,'username,userid');
		include self::admin_tpl('group_list');
	}
	
	/**
	 * 组员增加
	 */
	public function group_add(){
		if(isset($_POST['dosubmit'])){
			$user_group_db = new User_group_Model();
			$crewname = $this->check_crew($_POST['info']['crewname']);
			$info['userid'] = $crewname;		//填写组员号
			$info['parentid'] = $_POST['info']['userid'];//组长号
			$user_group_db->insert($info);
			self::showmessage('操作成功','?m=admin&c=groupmanage&a=grounp_manage');
		} else {
			$realname = CParam::get_cookie('realname');
			$userid = $_SESSION['userid']; //组长号
			include self::admin_tpl('group_add');
		}
	}
	/**
	 * 组员删除
	 * Enter description here ...
	 */
	public function group_delete(){
		$userid = intval($_GET['userid']);
		$user_group_db = new User_group_Model();
		$user_group_db->delete(array('userid'=>$userid));
		self::showmessage('操作成功','?m=admin&c=groupmanage&a=grounp_manage');
	}
	
	/**
	 * 异步检测crewname
	 * Enter description here ...
	 */
	public function public_crew_ajx(){
		$crewname = $_GET['crewname'];
		$admindb = new Admin_Model();
		$role_priv_db = new Admin_role_priv_Model();
		$user_group_db = new User_group_Model();
		$r = $admindb->get_one(array('username'=>$crewname),'userid,roleid');
		$priv = $role_priv_db->get_one(array('m'=>'admin','c'=>'groupmanage','a'=>'group_add','roleid'=>$r['roleid']));
		$parentid = $user_group_db->get_one(array('userid'=>$r['userid']),'parentid');
		if(!$r) 
		{
			exit('0');
		}elseif($priv){
			exit('0');
		} elseif ($r['roleid'] == '1'){
			exit('0');
		} elseif ($parentid){
			exit('0');
		}
		exit('1');
	}
	
	/**
	 * 组员名检查
	 */
	private function check_crew($crewname){
		$admindb = new Admin_Model();
		$role_priv_db = new Admin_role_priv_Model();
		$user_group_db = new User_group_Model();
		$r = $admindb->get_one(array('username'=>$crewname),'userid,roleid');
		$priv = $role_priv_db->get_one(array('m'=>'admin','c'=>'groupmanage','a'=>'group_add','roleid'=>$r['roleid']));
		$parentid = $user_group_db->get_one(array('userid'=>$r['userid']),'parentid');
		if(empty($crewname)){
			self::showmessage('组员不能为空！'); return false;
		}elseif(!$r) 
		{
			self::showmessage('该用户不存在！'); return false;
		}elseif($priv){
			self::showmessage('不能增加该用户！'); return false;
		} elseif ($r['roleid'] == '1'){
			self::showmessage('不能增加该用户！'); return false;
		} elseif ($parentid){
			$parent = $admindb->get_one(array('userid'=>$parentid['parentid']),'username,realname');
			self::showmessage('该用户组长为'.$parent['realname'].'('.$parent['username'].')!'); return false;
		}
		return $r['userid'];
	}
}